package com.chuanye.mydrawable.activitys;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.chuanye.mydrawable.R;
import com.chuanye.mydrawable.adapters.RepairInfoAdapter;
import com.chuanye.mydrawable.beans.RepairInfoBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

public class RepairRecordActivity extends AppCompatActivity implements View.OnClickListener{
    private Context mContext;

    private ListView rere_lv;
    private List<RepairInfoBean.ListBean> listBeanList = new ArrayList<>();
    private ImageView rere_iv_back;
    private ImageView rere_iv_add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_record);
        mContext = this;
        initView();
        initData();
    }
    private void initView() {
        rere_iv_back = (ImageView) findViewById(R.id.rere_iv_back);//返回
        rere_iv_add = (ImageView) findViewById(R.id.rere_iv_add);//添加
        rere_iv_add.setOnClickListener(this);
        rere_lv = (ListView) findViewById(R.id.rere_lv);
        rere_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String RepairId = listBeanList.get(i).getRepairId();
                Log.i("数据：",""+RepairId);
                Intent intent = new Intent(mContext,RepairDetailsActivity.class);
                intent.putExtra("RepairId",RepairId);
                startActivity(intent);
            }
        });

    }
    private void initData() {
        getRepairList();

    }

    /**
     * 获得报修数据列表
     */
    private void getRepairList() {
        OkHttpUtils.get()
                .url("http://172.16.2.77:9001/api/RepairInfo")//用户登陆并做与设备关系处理
                //.addParams("request.pageIndex", "0")//ZMX  LXQ
                //.addParams("request.pagesSize", String.valueOf(Integer.MAX_VALUE))//q123456
                .addParams("request.pageIndex", "1")//ZMX  LXQ
                .addParams("request.pagesSize", String.valueOf(Integer.MAX_VALUE))//q123456
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.i("请求异常：",e.toString());

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("报修列表请求数据：",response.toString());
                        Gson gson = new Gson();
                        String str = response.toString();
                        RepairInfoBean repairInfoBean = gson.fromJson(str,RepairInfoBean.class);
                        if (repairInfoBean.getList().size() > 0){
                            Log.i("刷新条数：",repairInfoBean.getList().size()+"");
                            listBeanList = repairInfoBean.getList();
                            RepairInfoAdapter adapter = new RepairInfoAdapter(mContext,listBeanList);
                            rere_lv.setAdapter(adapter);

                        }

                    }
                });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rere_iv_add://添加报修
                /*Intent intent = new Intent(mContext,CreateRepairsActivity.class);
                startActivity(intent);*/
                Intent intent = new Intent(mContext,QueryRepairActivity.class);
                startActivity(intent);
                break;
        }
    }
}
